package com.bjpowernode.config.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bjpowernode.DlykServerApplication;
import com.bjpowernode.model.TDicValue;
import com.bjpowernode.model.TProduct;
import com.bjpowernode.result.DicEnum;

import java.util.List;

/**
 * 意向产品转换器
 */
public class IntentionProductConverter implements Converter<Integer> {

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // cellData是Excel中读取到的数据,是'海豚', '秦PLUS EV'
        String cellIntentionProductName = cellData.getStringValue();

        // 从缓存中获取数据
        List<TProduct> tProductList = (List<TProduct>) DlykServerApplication.cacheMap.get(DicEnum.PRODUCT.getCode());
        for (TProduct tProduct : tProductList) {
            Integer id = tProduct.getId();
            String name = tProduct.getName();
            if (cellIntentionProductName.equalsIgnoreCase(name)) {
                return id;
            }
        }
        return -1;
    }
}
